home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ghostscript.idb / usr / freeware / lib / ghostscript / 3.33 / impath.ps.z / impath.ps
Encoding:
Text File  |  1998-05-21  |  5.2 KB  |  175 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of GNU Ghostscript.
  3. % GNU Ghostscript is distributed in the hope that it will be useful, but
  4. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  5. % anyone for the consequences of using it or for whether it serves any
  6. % particular purpose or works at all, unless he says so in writing.  Refer
  7. % to the GNU Ghostscript General Public License for full details.
  8.  
  9. % impath.ps
  10. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  11. % (This was formerly a Ghostscript operator implemented in C.)
  12.  
  13. %    <image> <width> <height> <wx> <wy> <ox> <oy> <string>
  14. %      type1imagepath <substring>
  15. %        Converts an image (bitmap) description of a character into
  16. %          a scalable description in Adobe Type 1 format.  The
  17. %          current transformation matrix should be the same as the
  18. %          FontMatrix of the font in which this character will be
  19. %          used: this establishes the scaling relationship between
  20. %          image pixels (the image is assumed to be 1 unit high in
  21. %          user space) and the character coordinate system used in
  22. %          the scalable description.  wx and wy are the character
  23. %          width, and ox and oy are the character origin relative
  24. %          to the lower left corner of the bitmap, in *pixels*.
  25. %        The image is assumed to be stored in left-to-right,
  26. %          top-to-bottom order.  Note that this is not consistent
  27. %          with the `image' operator's interpretation of the CTM.
  28. %        All coordinates in the scalable description are rounded to
  29. %          integers, so the coefficients in the FontMatrix should
  30. %          be on the order of 1/N for some value of N that is
  31. %          either a multiple of the height/width or is large
  32. %          compared to the width and height.  (There is a
  33. %          convention, which some PostScript programs rely on, that
  34. %          N=1000.)
  35. %        Note that the encoded description has *not* been subjected
  36. %          to CharString encryption, which is necessary before the
  37. %          description can be given to type1addpath: to do this,
  38. %          follow the type1imagepath with
  39. %            4330 exch dup .type1encrypt exch pop
  40. %        If the description is too complex to fit into the supplied
  41. %          string, a limitcheck error results.  A good rule of
  42. %          thumb is that the size of the string should be about 6
  43. %          times the number of 1-bits in the image that are not
  44. %          completely surrounded by other 1-bits.
  45.  
  46. % Import the Type 1 opcodes.
  47. (type1ops.ps) run
  48.  
  49. 100 dict
  50. dup /type1imagepath_dict exch def
  51. begin
  52.  
  53. /rc { round cvi } bind def
  54. /moving [/rmoveto /hmoveto /vmoveto] def
  55. /drawing [/rlineto /hlineto /vlineto] def
  56.  
  57. % Convert the current path to a Type 1 token array.
  58. /putxy            % x y ops -> cs_elements
  59.  { 3 -1 roll dup x sub rc exch /x exch def
  60.    3 -1 roll dup y sub rc exch /y exch def
  61.    % stack: ops dx dy
  62.    dup 0 eq
  63.     { % dy = 0, use hmoveto/lineto
  64.       pop exch 1 get
  65.     }
  66.     { 1 index 0 eq
  67.        { % dx = 0, use vmoveto/lineto
  68.          exch pop exch 2 get
  69.        }
  70.        { % use rmoveto/rlineto
  71.          3 -1 roll 0 get
  72.        }
  73.       ifelse
  74.     }
  75.    ifelse
  76.  } bind def
  77. /pathtotype1        % -> charstack
  78.  { 3 dict begin /x 0 def /y 0 def
  79.    mark
  80.    { moving putxy
  81.    }
  82.    { drawing putxy
  83.    }
  84.    { % Convert curve to relative form
  85.      x y 3
  86.       { exch neg 7 index add rc
  87.     exch neg 6 index add rc
  88.     8 -2 roll
  89.       }
  90.      repeat /y exch def /x exch def
  91.      1 index 0 eq 5 index 0 eq and    % dy1=dx3=0, hv
  92.       { 5 -1 roll pop exch pop /hvcurveto
  93.       }
  94.       { dup 0 eq 6 index 0 eq and    % dx1=dy3=0, vh
  95.          { 6 -1 roll pop pop /vhcurveto
  96.      }
  97.      { /rrcurveto            % none of the above
  98.      }
  99.     ifelse
  100.       }
  101.      ifelse
  102.    }
  103.    { /closepath
  104.    }
  105.    pathforall end
  106.  } bind def
  107.  
  108. end    % type1imagepath_dict
  109.  
  110. % The main program
  111. /type1imagepath        % image width height wx wy ox oy string ->
  112.             % substring
  113.  { type1imagepath_dict begin
  114.    /tsave save def
  115.    /ostring exch def
  116.    /oy exch def   /ox exch def
  117.    /wy exch def   /wx exch def
  118.    /height exch def   /width exch def
  119.    /data exch def
  120.  
  121.    /ofilter ostring /NullEncode filter def
  122.    /raster width 7 add 8 idiv def
  123.  
  124. % Construct the coordinate transformation.
  125.    height dup scale
  126.      matrix currentmatrix matrix invertmatrix setmatrix
  127.  
  128. % Determine the left side bearing.
  129.    /lsbx width
  130.    0 1 width 1 sub
  131.     { dup dup 8 idiv 0 exch
  132.       raster raster height mul 1 sub
  133.        { data exch get or }
  134.       for exch 8 mod bitshift 128 and 0 ne
  135.        { exch pop exit }
  136.       if pop
  137.     }
  138.    for def
  139.  
  140. % Compute and encode the origin, width, and side bearing.
  141.    mark
  142.    ox oy dtransform
  143.      rc /opty exch def   rc /optx exch def
  144.    wx wy dtransform
  145.      rc /iwy exch def    rc /iwx exch def
  146.    lsbx ox sub 0 dtransform
  147.      rc /ilsby exch def   rc /ilsbx exch def
  148.    ilsbx
  149.    iwy 0 ne ilsby 0 ne or
  150.     { ilsby iwx iwy /sbw }
  151.     { iwx /hsbw }
  152.    ifelse
  153.    ofilter charstack_write
  154.  
  155. % Flip over the Y axis, because the image is stored top-to-bottom.
  156.    [1 0 0 -1 0 height] concat
  157. % Account for the character origin.
  158.    lsbx oy translate
  159. % Trace the outline.
  160.    newpath
  161.    width height data .imagepath
  162.    gsave matrix setmatrix pathtotype1 grestore
  163.    ofilter charstack_write
  164. % Terminate the output
  165.    mark /endchar ofilter charstack_write
  166.  
  167.    ofilter fileposition ofilter closefile    % flush buffers
  168.    ostring 0 3 -1 roll getinterval
  169.    tsave restore
  170.    end
  171.  } bind def
  172.